package com.escape.subject.domain.service.impl;

import com.alibaba.fastjson.JSON;
import com.escape.subject.common.enums.CategoryTypeEnum;
import com.escape.subject.common.enums.IsDeletedEnum;
import com.escape.subject.domain.convert.SubjectCategoryConvert;
import com.escape.subject.domain.convert.SubjectLabelConvert;
import com.escape.subject.domain.entity.SubjectCategoryBO;
import com.escape.subject.domain.entity.SubjectLabelBO;
import com.escape.subject.domain.service.SubjectCategoryDomainService;
import com.escape.subject.domain.service.SubjectLabelDomainService;
import com.escape.subject.infra.basic.entity.SubjectCategory;
import com.escape.subject.infra.basic.entity.SubjectLabel;
import com.escape.subject.infra.basic.entity.SubjectMapping;
import com.escape.subject.infra.basic.service.SubjectCategoryService;
import com.escape.subject.infra.basic.service.SubjectLabelService;
import com.escape.subject.infra.basic.service.SubjectMappingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: escape_zebra
 * @CreateTime: 2024-12-18  12:12
 * @Description: TODO
 * @Version: 1.0
 */
@Slf4j
@Service
public class SubjectLabelDomainServiceImpl implements SubjectLabelDomainService {

    @Resource
    private SubjectLabelService subjectLabelService;

    @Resource
    private SubjectMappingService subjectMappingService;

    @Resource
    private SubjectCategoryService subjectCategoryService;

    /**
     * 添加标签
     * @param subjectLabelBO
     */
    @Override
    public void add(SubjectLabelBO subjectLabelBO) {
        if(log.isInfoEnabled()){
            log.info("SubjectLabelDomainService.add.BO:{}",JSON.toJSONString(subjectLabelBO));
        }
        // 转换
        SubjectLabel subjectLabel = SubjectLabelConvert.INSTANCE.convertBoToSubjectLabel(subjectLabelBO);
        subjectLabel.setIsDeleted(IsDeletedEnum.UN_DELETED.getCode());

        subjectLabelService.insert(subjectLabel);
    }

    /**
     * 更新标签
     * @param subjectLabelBO
     * @return
     */
    @Override
    public Boolean update(SubjectLabelBO subjectLabelBO) {
        if (log.isInfoEnabled()){
            log.info("SubjectLabelDomainService.update.BO: {}",JSON.toJSONString(subjectLabelBO));
        }

        // 1. 转换
        SubjectLabel subjectLabel = SubjectLabelConvert.INSTANCE.convertBoToSubjectLabel(subjectLabelBO);

        // 2. 更新
        int count = subjectLabelService.update(subjectLabel);

        return count > 0;
    }

    /**
     * 删除标签
     * @param subjectLabelBO
     * @return
     */
    @Override
    public Boolean delete(SubjectLabelBO subjectLabelBO) {
        if (log.isInfoEnabled()){
            log.info("SubjectLabelDomainService.delete.BO: {}",JSON.toJSONString(subjectLabelBO));
        }

        // 2. 转换
        SubjectLabel subjectLabel = SubjectLabelConvert.INSTANCE.convertBoToSubjectLabel(subjectLabelBO);

        // 1. 设置为已删除
        subjectLabel.setIsDeleted(IsDeletedEnum.DELETED.getCode());

        // 3. 删除
        int count = subjectLabelService.update(subjectLabel);

        return count > 0;
    }

    /**
     * 查询标签集合
     * @param subjectLabelBO
     * @return
     */
    @Override
    public List<SubjectLabelBO> queryLabelBO(SubjectLabelBO subjectLabelBO) {
        if (log.isInfoEnabled()){
            log.info("SubjectLabelDomainService.queryLabelBO.BO: {}",JSON.toJSONString(subjectLabelBO));
        }

        // 优先查询一级分类下的标签
        SubjectCategory subjectCategory = new SubjectCategory();
        subjectCategory.setId(subjectLabelBO.getCategoryId());
        SubjectCategory category = subjectCategoryService.queryByCondition(subjectCategory);
        if (CategoryTypeEnum.PRIMARY.getCode() == category.getCategoryType()) {
            SubjectLabel subjectLabel = new SubjectLabel();
            subjectLabel.setCategoryId(subjectLabelBO.getCategoryId());
            List<SubjectLabel> subjectLabelList = subjectLabelService.queryByCondition(subjectLabel);
            List<SubjectLabelBO> boList = SubjectLabelConvert.INSTANCE.convertListToBoList(subjectLabelList);
            return boList;
        }

        // 1. 获取分类ID
        Long categoryId = subjectLabelBO.getCategoryId();
        SubjectMapping subjectMapping = new SubjectMapping();
        subjectMapping.setCategoryId(categoryId);
        subjectMapping.setIsDeleted(IsDeletedEnum.UN_DELETED.getCode());

        // 2. 查询映射表中分类ID对应的标签ID
        List<SubjectMapping> subjectMappings = subjectMappingService.queryLabelIds(subjectMapping);

        if (CollectionUtils.isEmpty(subjectMappings)) {
            return Collections.emptyList();
        }

        // 3. 根据查询到的id查询标签
        List<Long> labelIds = subjectMappings.stream().map(item -> {
            return item.getLabelId();
        }).collect(Collectors.toList());

        // 4. 通过 ids 集合查询标签集合
        List<SubjectLabel> subjectLabels = subjectLabelService.queryLabelByIds(labelIds);

        // 5. 封装标签BO集合并返回
        LinkedList<SubjectLabelBO> boList = new LinkedList<>();
        subjectLabels.forEach(label -> {
            SubjectLabelBO labelBO = new SubjectLabelBO();
            labelBO.setId(label.getId());
            labelBO.setLabelName(label.getLabelName());
            labelBO.setSortNum(label.getSortNum());
            labelBO.setCategoryId(label.getCategoryId());
            boList.add(labelBO);
        });

        return boList;
    }
}
